<?php

namespace App\Http\Controllers\Admin\Wechat\Fun\Reply;

use Illuminate\Http\Request;
use App\Http\Controllers\AdminController;

use Validator;
use DB;
use App\Models\Wechat\Reply;
use App\Models\Config;

class OtherController extends AdminController
{
	/**
	 * 更新默认回复
	 * @return [type] [description]
	 */
	function updateOther(Request $request){
		$this->otherValidator($request);
		$config = new Config();
		$reply  = new Reply();
		$type = $request->input('type');
		$key = $reply->staticName[$type];	
		
		$config = $config->cacheFindByKey($key);
		if(!$config){
			$config = new Config();
		}
		$config->key = $key;
		$config->vue = json_encode([
			'wechat_mpnews_id' => $request->input('wechat_mpnews_id'),
			'wechat_image_id'  => $request->input('wechat_image_id'),
			'wechat_text_id'   => $request->input('wechat_text_id'),
		]);
		$config->type = $request->input('replyType');
		$config->cacheEmpty($key)->save();
		$this->ajaxJson(0,'保存成功');
	}

	/**
	 * 获取回复内容
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	function getReply(Request $request){
		$config = new Config();
		$reply  = new Reply();
		$type = $request->input('type');
		$key = $reply->staticName[$type];
		$config = $config->cacheFindByKey($key);
		if($config){
			$config->vue = json_decode($config->vue);
		}
		$this->ajaxJson(0,'',$config);
	}

	private function otherValidator($request){
		$validator = $this->formValidator($request->all());
        if($validator->fails()){
            $this->ajaxJson(1,$validator->errors()->first());
        }

        $replyType        = $request->input('replyType');
		$wechat_mpnews_id = $request->input('wechat_mpnews_id');
		$wechat_text_id   = $request->input('wechat_text_id');
		$wechat_image_id  = $request->input('wechat_image_id');
    	if($replyType=='mpnews'){
    		$mpnews = DB::table('wechat_mpnews')->find($wechat_mpnews_id);
    		if(!$mpnews){
    			$this->ajaxJson(1,'要添加的图文回复项已不存在,请重新选择');
    		}
    	}
    	if($replyType=='text'){
    		$text = DB::table('wechat_text')->find($wechat_text_id);
    		if(!$text){
    			$this->ajaxJson(1,'要添加的文本回复项已不存在,请重新选择');
    		}
    	}
    	if($replyType=='image'){
    		$image = DB::table('wechat_image')->find($wechat_image_id);
    		if(!$image){
    			$this->ajaxJson(1,'要添加的图片回复项已不存在,请重新选择');
    		}
    	}
	}

    /**
     * 表单验证
     * @param  array  $data [description]
     * @return [type]       [description]
     */
    private function formValidator(array $data){
        $attribute =  array(
			'type'             => '回复种类',
			'replyType'        => '回复内容',
			'wechat_text_id'   => '文本内容',
			'wechat_image_id'  => '图片内容',
			'wechat_mpnews_id' => '图文内容',
        );

        $message  = array(
            'required' => ':attribute必须填写',
            'in' => ':attribute数据错误,必须是 :values其中的一个',
            'required_if' => '您选择了:attribute却未指定数据,请求失败'
        );

        return Validator::make($data, [
			'type'             => 'bail|required|in:default,follow',
			'replyType'        => 'bail|required|in:mpnews,image,text',
			'wechat_text_id'   => 'required_if:replyType,text|numeric',
			'wechat_image_id'  => 'required_if:replyType,image|numeric',
			'wechat_mpnews_id' => 'required_if:replyType,mpnews|numeric',
        ],$message,$attribute);
    }
}
